﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ElementumForever.Interfaces;
using Microsoft.Xna.Framework.Input;
using ElementumForever.CommandPattern;
using Microsoft.Xna.Framework;
using ElementumForever.Utility;
using System.ComponentModel;

namespace ElementumForever.Editer
{
    public class MainFormModel//PresentationModel
    {
        public delegate void LoadFileHandle(string file);
        public event LoadFileHandle LoadFileEvent;
        public LuaEditerScene TargetScene { get; set; }
        private IsceneBuilder sceneModeler;
        public CommandManager CManager { get { return sceneModeler.CManager; } }
        public IsceneBuilder SceneModler { get { return sceneModeler; } set { sceneModeler = value; } }
        MainForm mainForm;


        public MainFormModel(MainForm form)
        {
            this.mainForm = form;
        }

        public void ChangeColor(Control cont)
        {
            ColorDialog MyDialog = new ColorDialog();
            MyDialog.AllowFullOpen = false;
            MyDialog.ShowHelp = true;
            MyDialog.Color = cont.ForeColor;
            if (MyDialog.ShowDialog() == DialogResult.OK)
                cont.BackColor = MyDialog.Color;
        }

        public void AddObjectByProperty(IEntityProperty properties)
        {
            ICommand command = new AddObjectCommand(TargetScene, properties.Name, properties.ObjectClass.ToString(),
                properties.Width, properties.Height, properties.OriginX, properties.OriginY, properties.ScaleX,
                properties.Rotation, properties.X, properties.Y, properties.Color);
            this.AddCommand(command);
        }

        public void AddCommand(ICommand command)
        {
            CManager.NewCommand(command);
            Update();
        }

        public Control Panel
        {
            get { return mainForm.Panel; }
        }

        public string readLua()
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "D:\\";
            openFileDialog1.Filter = "lua files (*.lua)|*.lua|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        if (LoadFileEvent != null)
                            LoadFileEvent(openFileDialog1.FileName);
                        Update();
                        myStream.Close();
                        return openFileDialog1.FileName;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
            return "open file fail";
        }

        public void NewScene()
        {
            LoadFileEvent("");
        }

        private void SaveAsLua(string contain)
        {
            Stream myStream;
            StreamWriter sw;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "lua files (*.lua)|*.lua|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    sw = new StreamWriter(myStream, System.Text.Encoding.Default);
                    sw.WriteLine(contain);
                    sw.Close();
                    myStream.Close();
                }
            }
        }

        public void OnMouseMove(object sender, MouseEventArgs e)
        {
            TargetScene.OnMouseMove(GetMouseState(e));
        }

        public void OnMouseDown(object sender, MouseEventArgs e)
        {
            TargetScene.OnMouseDown(GetMouseState(e));
        }

        public void OnMouseUp(object sender, MouseEventArgs e)
        {
            TargetScene.OnMouseUp(GetMouseState(e));
        }

        public void OnMouseClick(object sender, MouseEventArgs e)
        {
            TargetScene.OnMouseClick(GetMouseState(e));
        }

        public MouseState GetMouseState(MouseEventArgs e)
        {
            MouseState m = Mouse.GetState();
            Vector2 sv = new Vector2(e.X, e.Y);
            MouseState _state = new MouseState((int)sv.X, (int)sv.Y, m.ScrollWheelValue, m.LeftButton, m.MiddleButton, m.RightButton, m.XButton1, m.XButton2);
            return _state;
        }

        public void OnSelectEntity(BaseEntity entity)
        {
            if (entity == null)
            {
                SelectEntity = null;
                mainForm.SetObjectProperty(SelectEntity);
            }
            else
            {
                ObjectProperty property = new ObjectProperty(entity, this);//
                SelectEntity = entity;
                mainForm.SetObjectProperty(property.GetProperties());
                Console.WriteLine(entity.Name);
            }
        }

        public BaseEntity SelectEntity { get; set; }

        public void Update()
        {
            if (TargetScene != null)
            {
                ObjectProperty property = new ObjectProperty(TargetScene, this);//
                mainForm.SetSceneProperty(property.GetProperties());
                EntityInfo data = TargetScene.Serialize();
                mainForm.SetTreeData(data);
            }
            mainForm.UpdateStatus();
        }

        public void SaveAsLua()
        {
            string tableData = this.TargetScene.SceneDataString;
            this.SaveAsLua(tableData);
        }

        public void OpenItenMenu()
        {
            TargetScene.IsOpenItemMenu = !TargetScene.IsOpenItemMenu;
        }

        public void OnOpenContextMenu(object sender, CancelEventArgs e) {
            string text = "";

            if (SelectEntity != null)
                text = "OnOpenContextMenu:" + SelectEntity.Name;
            else
                text = "OnOpenContextMenu:miss";

            ContextMenuStrip contextMenuStrip = (ContextMenuStrip)sender;
            contextMenuStrip.Items.Clear();
            contextMenuStrip.Items.Add(text);
            contextMenuStrip.Items.Add("-");
            //contextMenuStrip.Items.Add("Apples");
            //contextMenuStrip.Items.Add("Oranges");
            //contextMenuStrip.Items.Add("Pears");

            ToolStripItem item = new ToolStripMenuItem();
            item.Name = "cutContextItem";
            item.Text = "cut";
            item.Tag = EditOptions.Cut;
            item.Click += OnEditOptionClick;

            contextMenuStrip.Items.Add(item);

            e.Cancel = false;
        }

        public void OnEditOptionClick(object sender, EventArgs e)
        {
            ToolStripItem item = (ToolStripItem)sender;
            EditOptions option = (EditOptions)item.Tag;
            string str = option.ToString();
            EditOption(option);
            if(SelectEntity!=null){
                str = SelectEntity.Name+":" + option.ToString();
            }
            MessageBox.Show(str);
        }

        public void EditOption(EditOptions option) {
        
        }

        public enum EditOptions
        {
            Copy,
            Paste,
            Cut
        }
    }
}
